package com.ch.swagger2.launch;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.nio.charset.StandardCharsets;

/**
 * Created by chenhao on 2017/7/4.
 * spring配置类
 */
@Configuration
public class SpringConfig {

    @Bean(name = "multipartResolver")
    public CommonsMultipartResolver commonsMultipartResolver() {
        CommonsMultipartResolver resolver = new CommonsMultipartResolver();
        //指定所上传文件的总大小不能超过5M。注意maxUploadSize属性的限制不是针对单个文件，而是所有文件的容量之和
        resolver.setMaxUploadSize(5242880L);
        resolver.setDefaultEncoding(StandardCharsets.UTF_8.name());
        //resolveLazily属性启用是为了推迟文件解析，以便捕获文件大小异常
        resolver.setResolveLazily(true);
        return resolver;
    }

    /**
     * Must register a static PropertySourcesPlaceholderConfigurer bean in either XML or annotation,
     * so that Spring @Value know how to interpret ${}
     *
     * @return
     */
    @Bean(name = "propertySourcesPlaceholderConfigurer")
    public static PropertySourcesPlaceholderConfigurer propertyConfigIn() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}
